package admin.Controller;

import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import admin.DTO.EmployeeDTO;
import admin.Service.EmployeeService;

@Controller
public class EmployeeController {
	private EmployeeService service;
	
	public EmployeeController(EmployeeService service){
		this.service = service;
	}
	
	@RequestMapping("/join.do")
	public String join(){
		return "employeeJoin.jo";
	}
	
	@RequestMapping("/search.do")
	public String search(){
		return "employeeSearch.se";
	}
	
	@RequestMapping("/login.do")
	public ModelAndView login(HttpSession session,EmployeeDTO dto){
		ModelAndView mv = new ModelAndView();
		try {
			EmployeeDTO edto=service.login(dto);
			if(edto!= null&&edto.getEmployee_yn().equals("Y")){
				if(dto.getEmployee_password().equals(edto.getEmployee_password())){
					session.setAttribute("login_info", edto);
					mv.setViewName("login.lo");
				}else{
					
				}
			}else{
				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return mv;
	}
	
	@RequestMapping("/loginOut.do")
	public ModelAndView loginOut(HttpSession session){
		ModelAndView mv = new ModelAndView();
		if(session!=null){
			session.invalidate();
			mv.setViewName("login.lo");
		}
		return mv;
	}
	
	@RequestMapping("/register.do")
	public ModelAndView register(EmployeeDTO dto){
		ModelAndView mv = new ModelAndView();
		try {
			service.employeeJoin(dto);
			mv.addObject("messege", "회원가입에 성공하셨습니다.");
			mv.setViewName("employeeJoin.jo");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return mv;
	}
	
	@RequestMapping("/searchEmployee.do")
	public ModelAndView searchEmployee(EmployeeDTO dto){
		ModelAndView mv = new ModelAndView();
		ArrayList<EmployeeDTO> list = new ArrayList<EmployeeDTO>();
		try {
			 list = service.searchEmployee(dto);
			 mv.addObject("list", list);
			 mv.setViewName("jsonView");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return mv;
	}
	
	@RequestMapping("/employeeDetial.do")
	public ModelAndView employeeDetial(EmployeeDTO dto){
		ModelAndView mv  = new ModelAndView();
		try {
			EmployeeDTO edto = service.employeeDetial(dto);
			mv.addObject("dto", edto);
			mv.setViewName("employeeDetial");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return mv;
	}
	
	@RequestMapping("/employee_modify.do")
	public String employeeModify(EmployeeDTO dto){
		@SuppressWarnings("unused")
		int i = 0;
		try {
			i=service.employeeModify(dto);
			return "a";
		}catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@RequestMapping("/delete.do")
	public ModelAndView employeeDelete(HttpServletRequest request){
		ModelAndView mv = new ModelAndView();
		String[] employee_no = request.getParameterValues("employee");
		int no = 0;
		try {
			for(int i=0; i<employee_no.length; i++){
				no = Integer.parseInt(employee_no[i]);
				service.employeeDelete(no);
			}
			mv.addObject("message","사원을 해고 하였습니다.");
			mv.setViewName("employeeSearch.se");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return mv;
	}
	
	@RequestMapping("/fire.do")
	public String employeeFire(EmployeeDTO dto){
		try {
			service.employeeDelete(dto.getEmployee_no());
			return "a";
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
}
















